package com.gpt.article.service.impl.taskHandle.testTask;

import chatgpt.ChatGPT;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSONArray;
import com.gpt.article.domain.GptTopicContent;
import com.gpt.article.domain.GptTopicTitle;
import com.gpt.article.domain.TxtClassify;
import com.gpt.article.service.IGptTopicContentService;
import com.gpt.article.service.IGptTopicTitleService;
import com.gpt.article.service.ITxtClassifyService;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.GptTxtClassDetail;import com.gpt.article.service.IGptTxtClassDetailService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.io.File;
import java.math.BigInteger;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class ClassTaskTest {
    private static IGptTxtClassDetailService gptTxtClassDetailService;
    private static ITxtClassifyService txtClassifyService;

    private static IGptTopicTitleService gptTopicTitleService;
    private static IGptTopicContentService gptTopicContentService;
    private static int counter = 0;
    @Autowired
    public void setGptTopicTitleService(IGptTopicTitleService gptTopicTitleService){
        ClassTaskTest.gptTopicTitleService = gptTopicTitleService;
    }

    @Autowired
    public void setGptTopicContentService(IGptTopicContentService gptTopicContentService){
        ClassTaskTest.gptTopicContentService = gptTopicContentService;
    }


    @Autowired
    public void setIGptTxtClassDetailService(IGptTxtClassDetailService gptTxtClassDetailService){
        ClassTaskTest.gptTxtClassDetailService = gptTxtClassDetailService;
    }

    @Autowired
    public void setITxtClassifyService(ITxtClassifyService txtClassifyService){
        ClassTaskTest.txtClassifyService = txtClassifyService;
    }


//    public static void main(String[] args) {
//        String classInfostr = "[{\"children\":[{\"children\":[],\"id\":176,\"label\":\"专业施工\"},{\"children\":[],\"id\":177,\"label\":\"交通运输工程\"},{\"children\":[],\"id\":178,\"label\":\"修缮、装饰工程\"},{\"children\":[],\"id\":179,\"label\":\"修缮工程\"},{\"children\":[],\"id\":180,\"label\":\"其他建筑工程\"},{\"children\":[],\"id\":181,\"label\":\"其它种类工程\"},{\"children\":[],\"id\":182,\"label\":\"工程准备\"},{\"children\":[],\"id\":183,\"label\":\"工程设备租赁（带操作员）\"},{\"children\":[],\"id\":184,\"label\":\"市政建设工程\"},{\"children\":[],\"id\":185,\"label\":\"建筑安装工程\"},{\"children\":[],\"id\":186,\"label\":\"建筑物\"},{\"children\":[],\"id\":187,\"label\":\"建筑物施工\"},{\"children\":[],\"id\":188,\"label\":\"构筑物施工\"},{\"children\":[],\"id\":189,\"label\":\"水利、防洪工程\"},{\"children\":[],\"id\":190,\"label\":\"油气工程\"},{\"children\":[],\"id\":191,\"label\":\"环保、绿化工程\"},{\"children\":[],\"id\":192,\"label\":\"电信工程\"},{\"children\":[],\"id\":193,\"label\":\"电力工程\"},{\"children\":[],\"id\":194,\"label\":\"系统集成、网络工程\"},{\"children\":[],\"id\":195,\"label\":\"装修工程\"},{\"children\":[],\"id\":196,\"label\":\"预制构件组装和装配\"}],\"id\":175,\"label\":\"工程\"},{\"children\":[{\"children\":[],\"id\":198,\"label\":\"专业咨询、工程监理、工程设计\"},{\"children\":[],\"id\":199,\"label\":\"专业技术服务\"},{\"children\":[],\"id\":200,\"label\":\"交通工具的维护保障\"},{\"children\":[],\"id\":201,\"label\":\"交通运输和仓储服务\"},{\"children\":[],\"id\":202,\"label\":\"会议\"},{\"children\":[],\"id\":203,\"label\":\"会议和展览服务\"},{\"children\":[],\"id\":204,\"label\":\"住宿和餐饮服务\"},{\"children\":[],\"id\":205,\"label\":\"保险\"},{\"children\":[],\"id\":206,\"label\":\"信息技术、信息管理软件的开发设计\"},{\"children\":[],\"id\":207,\"label\":\"信息技术服务\"},{\"children\":[],\"id\":208,\"label\":\"公共设施管理服务\"},{\"children\":[],\"id\":209,\"label\":\"其他服务\"},{\"children\":[],\"id\":210,\"label\":\"农林牧副渔服务\"},{\"children\":[],\"id\":211,\"label\":\"医疗卫生和社会服务\"},{\"children\":[],\"id\":212,\"label\":\"印刷、出版\"},{\"children\":[],\"id\":213,\"label\":\"商务服务\"},{\"children\":[],\"id\":214,\"label\":\"培训\"},{\"children\":[],\"id\":215,\"label\":\"工程咨询管理服务\"},{\"children\":[],\"id\":216,\"label\":\"房地产服务\"},{\"children\":[],\"id\":217,\"label\":\"批发和零售服务\"},{\"children\":[],\"id\":218,\"label\":\"教育服务\"},{\"children\":[],\"id\":219,\"label\":\"文化、体育、娱乐服务\"},{\"children\":[],\"id\":220,\"label\":\"水利管理服务\"},{\"children\":[],\"id\":221,\"label\":\"物业管理\"},{\"children\":[],\"id\":222,\"label\":\"环境服务\"},{\"children\":[],\"id\":223,\"label\":\"电信和其他信息传输服务\"},{\"children\":[],\"id\":224,\"label\":\"科学研究和试验开发\"},{\"children\":[],\"id\":225,\"label\":\"租赁\"},{\"children\":[],\"id\":226,\"label\":\"租赁服务（不带操作员）\"},{\"children\":[],\"id\":227,\"label\":\"维修\"},{\"children\":[],\"id\":228,\"label\":\"维修和保养服务\"},{\"children\":[],\"id\":229,\"label\":\"能源的生产和分配服务\"},{\"children\":[],\"id\":230,\"label\":\"采矿业和制造业服务\"},{\"children\":[],\"id\":231,\"label\":\"金融服务\"}],\"id\":197,\"label\":\"服务\"},{\"children\":[{\"children\":[],\"id\":233,\"label\":\"一般设备\"},{\"children\":[],\"id\":234,\"label\":\"专用材料\"},{\"children\":[],\"id\":235,\"label\":\"专用设备\"},{\"children\":[],\"id\":236,\"label\":\"交通工具\"},{\"children\":[],\"id\":237,\"label\":\"其他货物\"},{\"children\":[],\"id\":238,\"label\":\"农林牧渔业产品\"},{\"children\":[],\"id\":239,\"label\":\"办公消耗用品\"},{\"children\":[],\"id\":240,\"label\":\"办公消耗用品及类似物品\"},{\"children\":[],\"id\":241,\"label\":\"医药品\"},{\"children\":[],\"id\":242,\"label\":\"图书和档案\"},{\"children\":[],\"id\":243,\"label\":\"土地\"},{\"children\":[],\"id\":244,\"label\":\"土地、建筑物及构筑物\"},{\"children\":[],\"id\":245,\"label\":\"基础化学品及相关产品\"},{\"children\":[],\"id\":246,\"label\":\"家具用具\"},{\"children\":[],\"id\":247,\"label\":\"建筑、装饰材料\"},{\"children\":[],\"id\":248,\"label\":\"建筑建材\"},{\"children\":[],\"id\":249,\"label\":\"文物和陈列品\"},{\"children\":[],\"id\":250,\"label\":\"无形资产\"},{\"children\":[],\"id\":251,\"label\":\"橡胶、塑料、玻璃和陶瓷制品\"},{\"children\":[],\"id\":252,\"label\":\"炼焦产品、炼油产品\"},{\"children\":[],\"id\":253,\"label\":\"物资\"},{\"children\":[],\"id\":254,\"label\":\"电力、城市燃气、蒸汽和热水、水\"},{\"children\":[],\"id\":255,\"label\":\"矿与矿物\"},{\"children\":[],\"id\":256,\"label\":\"纸、纸制品及印刷品\"},{\"children\":[],\"id\":257,\"label\":\"纺织原料、毛皮、被服装具\"},{\"children\":[],\"id\":258,\"label\":\"通用设备\"},{\"children\":[],\"id\":259,\"label\":\"食品、饮料和烟草原料\"}],\"id\":232,\"label\":\"货物\"}]";
//        List<TreeSelect> classInfos = JSONArray.parseArray(classInfostr,TreeSelect.class);
//        String s1 = "服务2";
//        String s2 = "信息技术服务2";
//
//        List<TreeSelect> oneTree = findChildrenByLabel(classInfos,s1);
//        List<TreeSelect> twoTree = findChildrenByLabel(classInfos,s2);
////        oneTree.forEach(s -> System.out.println(s.getChildren().size()));
//        System.out.println(oneTree);
//        System.out.println(twoTree);
//
//    }



    public static void testpost(){
        //构建gpt
        ChatGPT chatGPT = ChatGPT.builder()
//                .apiKey("sk-aKa3gJyp4eGZLpNF6b6190De2b904fEd9bCeA80452Ec34Cf")
//                .proxy(proxy)
                .apiHost("http://ai.qujiami.cn/api/openai/v1/chat/completions") //代理地址
                .build()
                .init();

        String res = chatGPT.chat("你是什么模型？");
        System.out.println(res);
    }

    public static void main(String[] args) {
        xibanyayu();
    }


    /**
     * 在一个长字符串中找出所有的<company>到</a></p> 之间的字符串
     */
    public static void getChongfu(){
       List<GptTopicContent> gptTopicContentList =  gptTopicContentService.selectGptTopicContentList(new GptTopicContent());

       int test = 0;
       for(GptTopicContent gptTopicContent:gptTopicContentList){
           String genContent2 = gptTopicContent.getGenContent();
           String genContent = gptTopicContent.getGenContent();
//           String reg = "(?<=<company>).*?(?=<\\/a><\\/p>)";
//           String reg = "<company>.*?<\\/a><\\/p>";
           String reg = "<company>.*?<\\/a><\\/p>";
//           genContent = genContent.replaceAll("\n","");
           Pattern pattern = Pattern.compile(reg,Pattern.DOTALL);
           Matcher matcher = pattern.matcher(genContent);
           Long customId= gptTopicContent.getCustomId();
//           System.out.println(customId+"--CustomId->");


           while (matcher.find()) {
               String regString = matcher.group();
               //查询多少个
               counter = 0;
              int count =  countStr(genContent,regString);
               if(count>1){
                   try{
//                       genContent =genContent.replaceFirst(regString,"");
                       genContent =removeDuplicateString(genContent,regString);

                   }catch (Exception e){
                       System.out.println("错误："+e.getMessage());
                   }
test++;
                   System.out.println("出现个数："+count+",,CustomId:"+gptTopicContent.getCustomId()+",,公司字符串："+regString  );

               }
           }
           System.out.println("操作对象：'"+gptTopicContent.getTitle()+"'，是否替换："+genContent2.equals(genContent));
           gptTopicContent.setGenContent(genContent);
           gptTopicContentService.updateGptTopicContent(gptTopicContent);

//           System.out.println("~~~~~~~~~~~~~");
       }
        System.out.println(test+"：：");
    }


    //去除后面的重复
    public static String removeDuplicateString(String text, String toRemove) {
        // Escape special characters in toRemove string
        String escapedToRemove = java.util.regex.Pattern.quote(toRemove);

        // Build the regex that matches toRemove string but not the first occurrence
        String regex = "(?s)" + escapedToRemove;

        // Replace all found duplicates after the first occurrence
        return text.replaceFirst(regex, "").replaceAll(regex, "");
    }

    public static int countStr(String str1, String str2) {
        if (str1.indexOf(str2) == -1) {
            return 0;
        } else if (str1.indexOf(str2) != -1) {
            counter++;
            countStr(str1.substring(str1.indexOf(str2) +
                    str2.length()), str2);
            return counter;
        }
        return 0;
    }

    /**
     * 处理西班牙语
     */
    public static void xibanyayu(){
        String matExcelPath = "C:\\Users\\竹子\\Desktop\\content\\法语.xlsx";
        List<Map<String,Object>> dataList = getDataMapdata("E:\\git-test\\kehu\\xz\\小语种\\法语翻译\\爬的3950法语译文.xlsx");
        File duibiExccelFile = new File(matExcelPath);
        ExcelReader duibireader = cn.hutool.poi.excel.ExcelUtil.getReader(duibiExccelFile);
        List<Map<String, Object>> duibimaps = duibireader.readAll();
        String names = "";
        String Catalogue = "";
        String SummaryA = "";
        String SummaryA_ex = "";
        String PRT_Words = "";
        String ID = "";
        GptTopicContent gptTopicContent = null;
        GptTopicTitle gptTopicTitle = null;
        loop1:for(Map<String,Object> map:duibimaps){
            names = String.valueOf(map.get("Name"));
            Catalogue = String.valueOf(map.get("Catalogue"));
            SummaryA = String.valueOf(map.get("SummaryA"));
            SummaryA_ex = String.valueOf(map.get("SummaryA_ex"));
            PRT_Words = String.valueOf(map.get("PRT_Words"));
            ID = String.valueOf(map.get("ID"));


            gptTopicTitle = new GptTopicTitle();
            gptTopicTitle.setTitle(names);
            gptTopicTitle.setClassId(6L);
            gptTopicTitle.setClassName("工业");
            gptTopicTitle.setOutlineId(9L);
            gptTopicTitle.setOutlineName("法语-产品");
            //excel查找
            boolean isfind = false;
            loop2: for(Map<String,Object> commap:dataList){
                String t = String.valueOf(commap.get("Title"));
                String Companies = String.valueOf(commap.get("Companies "));
                if(names.equals(t)){
                    isfind = true;
                    gptTopicTitle.setDemandPrompts(Companies);
                    break loop2;
                }
            }
            gptTopicTitle.setStatus("0");
            if(!isfind){
                gptTopicTitle.setDemandPrompts(null);
                gptTopicTitle.setStatus("1");
            }
//            gptTopicTitle.setDemandPrompts("");

            gptTopicTitle.setLanguage("法语");
            gptTopicTitle.setUseModel("gpt-3.5-turbo");
            gptTopicTitle.setCreateTime(new Date());
           Long titleId =   gptTopicTitleService.insertGptTopicTitle(gptTopicTitle);


            gptTopicContent = new GptTopicContent();
            gptTopicContent.setTitle(names);
            gptTopicContent.setGenCatalog(Catalogue);
            gptTopicContent.setGenContent(SummaryA+SummaryA_ex);
            gptTopicContent.setKeyWords(PRT_Words);
            gptTopicContent.setCustomId(titleId);
            Long contentid =  gptTopicContentService.insertGptTopicContent(gptTopicContent);
            gptTopicTitle.setContentId(contentid);
            gptTopicTitleService.updateGptTopicTitle(gptTopicTitle);

        }

    }

    /**
     * 读取excel表中数据
     * @param matExcelPath
     * @return
     */
    public static List<Map<String,Object>> getDataMapdata(String matExcelPath){
        File duibiExccelFile = new File(matExcelPath);
        ExcelReader duibireader = cn.hutool.poi.excel.ExcelUtil.getReader(duibiExccelFile);
        List<Map<String, Object>> dataList  = duibireader.readAll();
        return dataList;
    }










    /**
     * 处理遗留的一些公司数据问题
     */
    public static void fxexcel(){
        String mainExccel = "C:\\Users\\竹子\\Desktop\\content\\西班牙.xlsx";
        String matExcelPath = "C:\\Users\\竹子\\Desktop\\content\\西班牙2.xlsx";
        File duibiExccelFile = new File(mainExccel);
        ExcelReader duibireader = cn.hutool.poi.excel.ExcelUtil.getReader(duibiExccelFile);
        List<Map<String, Object>> duibimaps = duibireader.readAll();
        String names = "";
        String Catalogue = "";
        String SummaryA = "";
        String SummaryA_ex = "";
        String PRT_Words = "";
        String ID = "";
        int num = 0;
        String titles = "<h3 id=\"section1\">Algunas empresas clave de {title}, su breve introducción y sitio web oficial</h3>";
        ArrayList<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
        Map<String, Object> keyMap  = null;
        for(Map<String,Object> map:duibimaps){
             names = String.valueOf(map.get("Name"));
             Catalogue = String.valueOf(map.get("Catalogue"));
             SummaryA = String.valueOf(map.get("SummaryA"));
             SummaryA_ex = String.valueOf(map.get("SummaryA_ex"));
             PRT_Words = String.valueOf(map.get("PRT_Words"));
             ID = String.valueOf(map.get("ID"));
            String reg = "(?<=<h3 id=\"section1\">).*?(?=<p>)";
            String mt = getMatstr(reg,SummaryA);
            if(mt.length()<10){
                System.out.println(mt);
                String er = "<h3 id=\"section1\">"+mt+"<p>";
                titles = titles.replace("{title}",names);
                SummaryA = SummaryA.replace(er,titles);
                keyMap = new LinkedHashMap<>();
                keyMap.put("Name",StringUtils.isEmpty(names)||"null".equals(names)?"":names);
                keyMap.put("Catalogue",StringUtils.isEmpty(Catalogue)||"null".equals(Catalogue)?"":Catalogue);
                keyMap.put("SummaryA",StringUtils.isEmpty(SummaryA)||"null".equals(SummaryA)?"":SummaryA);
                keyMap.put("SummaryA_ex",StringUtils.isEmpty(SummaryA_ex)||"null".equals(SummaryA_ex)?"":SummaryA_ex);
                keyMap.put("PRT_Words",StringUtils.isEmpty(PRT_Words)||"null".equals(PRT_Words)?"":PRT_Words);
                keyMap.put("ID",ID);
                rows.add(keyMap);

                num++;
            }

        }
        //创建excel
        ExcelWriter writer = cn.hutool.poi.excel.ExcelUtil.getWriter(matExcelPath);
// 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);
// 关闭writer，释放内存
        writer.close();


        System.out.println(num+"~~~~~~");

    }

    public static String getMatstr(String reg, String content){
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(content);
        String regString = "";
        if( matcher.find() ){
            regString = matcher.group();
        }
        return regString;
    }

    public static void matExcelData(){
        String spiderExcelPath = "E:\\git-test\\Lawer\\all_lic.xlsx";
        String matExcelPath = "E:\\git-test\\Lawer\\mat_law.xlsx";
        List<String> licList = genExcelData(spiderExcelPath);
        List<String> matlicList = genExcelData(matExcelPath);

        //对比两个list的不同，组成新的集合
//        List<String> newlicList = new ArrayList<String>();
        ArrayList<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();

        Map<String,Object> keyMap =null;
        for(String lic:matlicList){
            if(!licList.contains(lic)){
                keyMap = new HashMap<String,Object>();
                System.out.println(lic);
                keyMap.put("licnumber",lic);
                rows.add(keyMap);
//                newlicList.add(lic);
            }
        }
        //写入excel
        //创建excel
        ExcelWriter writer = cn.hutool.poi.excel.ExcelUtil.getWriter("E:\\git-test\\Lawer\\new_mat.xlsx");
// 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);
// 关闭writer，释放内存
        writer.close();



    }
    public static List<String>  genExcelData(String mainExccel){
        File duibiExccelFile = new File(mainExccel);
        ExcelReader duibireader = cn.hutool.poi.excel.ExcelUtil.getReader(duibiExccelFile);
        List<Map<String, Object>> duibimaps = duibireader.readAll();
        Map<String, Object> row1 = null;

        List<String> spiderList = new ArrayList<String>();
        for(Map dmap:duibimaps) {
            String licnumber = String.valueOf(dmap.get("licnumber"));
            spiderList.add(licnumber);
        }
        return spiderList;
    }





    public static void genCompnyName(){
        String mainExccel = "E:\\git-test\\Lawer\\techcompany.xlsx";
        File duibiExccelFile = new File(mainExccel);
        ExcelReader duibireader = cn.hutool.poi.excel.ExcelUtil.getReader(duibiExccelFile);
        List<Map<String, Object>> duibimaps = duibireader.readAll();
        Map<String, Object> row1 = null;
        ArrayList<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
        //构建gpt
        ChatGPT chatGPT = ChatGPT.builder()
                .apiKey("sk-aKa3gJyp4eGZLpNF6b6190De2b904fEd9bCeA80452Ec34Cf")

//                .proxy(proxy)
                .apiHost("https://api.glm-ai.com/") //代理地址
                .build()
                .init();


        loo2:for(Map dmap:duibimaps) {
            String company = String.valueOf(dmap.get("Company"));
            String res = chatGPT.chat("公司名："+company+"；请给出该公司人们口语或者通俗叫法的公司名称，包括英文名、中文名 ，无需任何解释，也不用特殊格式，多个用、号隔开即可，回答模版【Apple Computer, Inc.、Apple、苹果、苹果公司】方式回答");
            System.out.println(res);
            row1 = new HashMap<String, Object>();
            row1.put("nickName",row1);
            rows.add(row1);
//            break ;
        }

        //创建excel
        ExcelWriter writer = cn.hutool.poi.excel.ExcelUtil.getWriter("E:\\git-test\\Lawer\\companyName.xlsx");
// 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);
// 关闭writer，释放内存
        writer.close();
    }



    public static Boolean checkExcel(){
        String mainExccel = "C:\\Users\\竹子\\Desktop\\截图\\对比\\spider.xlsx";
        String duibi = "C:\\Users\\竹子\\Desktop\\截图\\对比\\law.xlsx";

        File duibiExccelFile = new File(duibi);
        ExcelReader duibireader = cn.hutool.poi.excel.ExcelUtil.getReader(duibiExccelFile);
        List<Map<String, Object>> duibimaps = duibireader.readAll();

        File mainExccelFile = new File(mainExccel);
        ExcelReader mainreader = cn.hutool.poi.excel.ExcelUtil.getReader(mainExccelFile);
        List<Map<String, Object>> maps = mainreader.readAll();
        int i= 0;
        ArrayList<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();

        Map<String,Object> keyMap = new HashMap<String,Object>();

        Map<String,Object> mapstr = null;
        loo1:for(Map map:maps) {
            String law_firm = String.valueOf(map.get("law_firm"));
            String licnumber = String.valueOf(map.get("licnumber"));
            String law_name = String.valueOf(map.get("law_name"));
//            System.out.println(law_firm);
//            System.out.println(licnumber);
//            System.out.println(law_name);
            String matchstr = law_firm+law_name+licnumber.trim().substring(licnumber.length()-3,licnumber.length()-1);
            Boolean isFind = false;
            loo2:for(Map dmap:duibimaps) {
                String law_firm2 = String.valueOf(dmap.get("law_firm"));
                String licnumber2 = String.valueOf(dmap.get("licnumber"));
                String law_name2 = String.valueOf(dmap.get("law_name"));
                String matchstr2 = law_firm2+law_name2+licnumber2.trim().substring(licnumber2.length()-2,licnumber2.length());
                if(matchstr.equals(matchstr2)){
                    isFind = true;
                    break loo2;
                }
            }
            if(!isFind){
                if(keyMap.get("licnumber")==null){
                    System.out.println("发现不存在的数据："+matchstr);
                    mapstr = new HashMap<>();
                    mapstr.put("law_firm",law_firm);
                    mapstr.put("licnumber",licnumber);
                    mapstr.put("law_name",law_name);
                    rows.add(mapstr);
                    keyMap.put(licnumber,null);
                }

            }
        }


        //创建excel
        ExcelWriter writer = cn.hutool.poi.excel.ExcelUtil.getWriter("d:/match_law.xlsx");
// 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);
// 关闭writer，释放内存
        writer.close();

        return true;
    }



    public Boolean checkClassNames(){
        List<TxtClassify> txtClassifies = txtClassifyService.selectTxtClassifyList(new TxtClassify());
        String classNames = "";
        String classInfostr = "[{\"children\":[{\"children\":[],\"id\":176,\"label\":\"专业施工\"},{\"children\":[],\"id\":177,\"label\":\"交通运输工程\"},{\"children\":[],\"id\":178,\"label\":\"修缮、装饰工程\"},{\"children\":[],\"id\":179,\"label\":\"修缮工程\"},{\"children\":[],\"id\":180,\"label\":\"其他建筑工程\"},{\"children\":[],\"id\":181,\"label\":\"其它种类工程\"},{\"children\":[],\"id\":182,\"label\":\"工程准备\"},{\"children\":[],\"id\":183,\"label\":\"工程设备租赁（带操作员）\"},{\"children\":[],\"id\":184,\"label\":\"市政建设工程\"},{\"children\":[],\"id\":185,\"label\":\"建筑安装工程\"},{\"children\":[],\"id\":186,\"label\":\"建筑物\"},{\"children\":[],\"id\":187,\"label\":\"建筑物施工\"},{\"children\":[],\"id\":188,\"label\":\"构筑物施工\"},{\"children\":[],\"id\":189,\"label\":\"水利、防洪工程\"},{\"children\":[],\"id\":190,\"label\":\"油气工程\"},{\"children\":[],\"id\":191,\"label\":\"环保、绿化工程\"},{\"children\":[],\"id\":192,\"label\":\"电信工程\"},{\"children\":[],\"id\":193,\"label\":\"电力工程\"},{\"children\":[],\"id\":194,\"label\":\"系统集成、网络工程\"},{\"children\":[],\"id\":195,\"label\":\"装修工程\"},{\"children\":[],\"id\":196,\"label\":\"预制构件组装和装配\"}],\"id\":175,\"label\":\"工程\"},{\"children\":[{\"children\":[],\"id\":198,\"label\":\"专业咨询工程监理工程设计\"},{\"children\":[],\"id\":199,\"label\":\"专业技术服务\"},{\"children\":[],\"id\":200,\"label\":\"交通工具的维护保障\"},{\"children\":[],\"id\":201,\"label\":\"交通运输和仓储服务\"},{\"children\":[],\"id\":202,\"label\":\"会议\"},{\"children\":[],\"id\":203,\"label\":\"会议和展览服务\"},{\"children\":[],\"id\":204,\"label\":\"住宿和餐饮服务\"},{\"children\":[],\"id\":205,\"label\":\"保险\"},{\"children\":[],\"id\":206,\"label\":\"信息技术、信息管理软件的开发设计\"},{\"children\":[],\"id\":207,\"label\":\"信息技术服务\"},{\"children\":[],\"id\":208,\"label\":\"公共设施管理服务\"},{\"children\":[],\"id\":209,\"label\":\"其他服务\"},{\"children\":[],\"id\":210,\"label\":\"农林牧副渔服务\"},{\"children\":[],\"id\":211,\"label\":\"医疗卫生和社会服务\"},{\"children\":[],\"id\":212,\"label\":\"印刷、出版\"},{\"children\":[],\"id\":213,\"label\":\"商务服务\"},{\"children\":[],\"id\":214,\"label\":\"培训\"},{\"children\":[],\"id\":215,\"label\":\"工程咨询管理服务\"},{\"children\":[],\"id\":216,\"label\":\"房地产服务\"},{\"children\":[],\"id\":217,\"label\":\"批发和零售服务\"},{\"children\":[],\"id\":218,\"label\":\"教育服务\"},{\"children\":[],\"id\":219,\"label\":\"文化、体育、娱乐服务\"},{\"children\":[],\"id\":220,\"label\":\"水利管理服务\"},{\"children\":[],\"id\":221,\"label\":\"物业管理\"},{\"children\":[],\"id\":222,\"label\":\"环境服务\"},{\"children\":[],\"id\":223,\"label\":\"电信和其他信息传输服务\"},{\"children\":[],\"id\":224,\"label\":\"科学研究和试验开发\"},{\"children\":[],\"id\":225,\"label\":\"租赁\"},{\"children\":[],\"id\":226,\"label\":\"租赁服务（不带操作员）\"},{\"children\":[],\"id\":227,\"label\":\"维修\"},{\"children\":[],\"id\":228,\"label\":\"维修和保养服务\"},{\"children\":[],\"id\":229,\"label\":\"能源的生产和分配服务\"},{\"children\":[],\"id\":230,\"label\":\"采矿业和制造业服务\"},{\"children\":[],\"id\":231,\"label\":\"金融服务\"}],\"id\":197,\"label\":\"服务\"},{\"children\":[{\"children\":[],\"id\":233,\"label\":\"一般设备\"},{\"children\":[],\"id\":234,\"label\":\"专用材料\"},{\"children\":[],\"id\":235,\"label\":\"专用设备\"},{\"children\":[],\"id\":236,\"label\":\"交通工具\"},{\"children\":[],\"id\":237,\"label\":\"其他货物\"},{\"children\":[],\"id\":238,\"label\":\"农林牧渔业产品\"},{\"children\":[],\"id\":239,\"label\":\"办公消耗用品\"},{\"children\":[],\"id\":240,\"label\":\"办公消耗用品及类似物品\"},{\"children\":[],\"id\":241,\"label\":\"医药品\"},{\"children\":[],\"id\":242,\"label\":\"图书和档案\"},{\"children\":[],\"id\":243,\"label\":\"土地\"},{\"children\":[],\"id\":244,\"label\":\"土地、建筑物及构筑物\"},{\"children\":[],\"id\":245,\"label\":\"基础化学品及相关产品\"},{\"children\":[],\"id\":246,\"label\":\"家具用具\"},{\"children\":[],\"id\":247,\"label\":\"建筑、装饰材料\"},{\"children\":[],\"id\":248,\"label\":\"建筑建材\"},{\"children\":[],\"id\":249,\"label\":\"文物和陈列品\"},{\"children\":[],\"id\":250,\"label\":\"无形资产\"},{\"children\":[],\"id\":251,\"label\":\"橡胶、塑料、玻璃和陶瓷制品\"},{\"children\":[],\"id\":252,\"label\":\"炼焦产品、炼油产品\"},{\"children\":[],\"id\":253,\"label\":\"物资\"},{\"children\":[],\"id\":254,\"label\":\"电力、城市燃气、蒸汽和热水、水\"},{\"children\":[],\"id\":255,\"label\":\"矿与矿物\"},{\"children\":[],\"id\":256,\"label\":\"纸、纸制品及印刷品\"},{\"children\":[],\"id\":257,\"label\":\"纺织原料、毛皮、被服装具\"},{\"children\":[],\"id\":258,\"label\":\"通用设备\"},{\"children\":[],\"id\":259,\"label\":\"食品、饮料和烟草原料\"}],\"id\":232,\"label\":\"货物\"}]";
        List<TreeSelect> classInfos = JSONArray.parseArray(classInfostr,TreeSelect.class);
        int j = 0;
        loop1:for(TxtClassify txtClassify:txtClassifies){
            classNames = txtClassify.getClassNames();
            String cs[] = classNames.split(">");
            if(cs.length!=2){
                System.out.println(txtClassify.getTxtContent()+"--二级分类有错："+txtClassify.getClassNames());
                txtClassify.setStatus("0");
                txtClassifyService.updateTxtClassify(txtClassify);
                continue ;
            }
            //查询分类是否存在
            String oneClassName = cs[0];
            String twoClassName = cs[1];
            List<TreeSelect> oneTree = findChildrenByLabel(classInfos,oneClassName);
            List<TreeSelect> twoTree = findChildrenByLabel(classInfos,twoClassName);
            if(oneTree!=null&&oneTree.size()!=0){
                if(twoTree==null){
                    System.out.println(txtClassify.getTxtContent()+"--二级分类有错："+twoTree);
                    txtClassify.setStatus("0");
                    txtClassifyService.updateTxtClassify(txtClassify);
                    continue ;
                }
            }else{
                System.out.println(txtClassify.getTxtContent()+"--二级分类有错："+twoTree);
                txtClassify.setStatus("0");
                txtClassifyService.updateTxtClassify(txtClassify);
                continue ;
            }
//            txtClassifyService.sele


//            loop2:for(String cstr:cs){
//
//            }


        }
        return true;
    }

    // 递归方法，用于查找节点并返回其子节点
    public static List<TreeSelect> findChildrenByLabel(List<TreeSelect> nodes, String label) {
        if (nodes == null) {
            return null;
        }
        for (TreeSelect node : nodes) {
            if (node.getLabel().equals(label) ) {
                return node.getChildren(); // 找到节点，返回其子节点
            }
            List<TreeSelect> children = findChildrenByLabel(node.getChildren(), label);
            if (children != null) {
                return children; // 在子节点中找到，返回结果
            }
        }
        return null; // 没有找到节点
    }


    public Boolean writeExcel(){
        List<TxtClassify> txtClassifies = txtClassifyService.selectTxtClassifyList(new TxtClassify());
        
        String category = "";
        Map<String, Object> row1 = null;
        ArrayList<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
        for(TxtClassify txt:txtClassifies){

            row1 = new LinkedHashMap<>();
            row1.put("title",txt.getTxtContent());
            category = txt.getClassNames();
            String ca[] = category.split(">");
            row1.put("category1",ca[0]);
            row1.put("category21",ca[1]);
            rows.add(row1);
        }

        //创建excel
        ExcelWriter writer = cn.hutool.poi.excel.ExcelUtil.getWriter("d:/class_info.xlsx");
// 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);
// 关闭writer，释放内存
        writer.close();
        return true;
    }






    public  Boolean importExl() {
//        GptTxtClassDetail gptTxtClassDetail  = new GptTxtClassDetail();
//        gptTxtClassDetail.setName("测试");
//        gptTxtClassDetailService.insertGptTxtClassDetail(gptTxtClassDetail);
        String filePath = "E:\\git-test\\class_task\\已分类.xlsx";
        File excelFile = new File(filePath);
        ExcelReader reader = cn.hutool.poi.excel.ExcelUtil.getReader(excelFile);
        List<Map<String, Object>> maps = reader.readAll();

        Map<String,Object> mapOne = new HashMap<String,Object>();
        Map<String,Object> mapTwo = new HashMap<String,Object>();

        GptTxtClassDetail gptTxtClassDetail  = null;
        Long itemId = Long.valueOf("1");
        int no = 1;

        Long category1Id=null;
        for(Map map:maps) {
            boolean needOne = false;
            boolean needTwo = false;
            String category1 = String.valueOf(map.get("category1"));
            String category21 = String.valueOf(map.get("category21"));
            //新增一级分类
            if(mapOne.get(category1)==null&& !StringUtils.isEmpty(category1)){
                needOne = true;
                mapOne.put(category1,category1);
                gptTxtClassDetail= new GptTxtClassDetail();
                gptTxtClassDetail.setItemId(itemId);
                gptTxtClassDetail.setParentId(Long.valueOf("0"));
                gptTxtClassDetail.setName(category1);
                gptTxtClassDetail.setStatus("0");
                gptTxtClassDetail.setOrderNum(no);
                category1Id = gptTxtClassDetailService.insertGptTxtClassDetail(gptTxtClassDetail);
                System.out.println("主类id："+category1Id);
                no++;
            }

            int no2 = 1;
            //新增二级分类
            if(mapTwo.get(category21)==null&& !StringUtils.isEmpty(category21)){
                needTwo = true;
                mapTwo.put(category21,category21);
                gptTxtClassDetail= new GptTxtClassDetail();
                gptTxtClassDetail.setItemId(itemId);
                gptTxtClassDetail.setParentId(category1Id);
                gptTxtClassDetail.setName(category21);
                gptTxtClassDetail.setStatus("0");
                gptTxtClassDetail.setOrderNum(no2);
                gptTxtClassDetailService.insertGptTxtClassDetail(gptTxtClassDetail);
                no2++;
            }
//
//            Set<String> set = map.keySet();
//            System.out.println(map+"----------");
//            for (String key : set) {
//                System.out.println(key+"~~");
//            }
        }
        return true;

    }

}
